Skip to content

Optimize TextRangeCollection.getItemContaining#11265

Open
Zaczero wants to merge 1 commit into
microsoft:mainfrom
Zaczero:zaczero/gic
Open

Optimize TextRangeCollection.getItemContaining#11265
Zaczero wants to merge 1 commit into
microsoft:mainfrom
Zaczero:zaczero/gic

Conversation

@Zaczero
Copy link
Copy Markdown
Contributor

@Zaczero Zaczero commented Feb 2, 2026

This change improved the performance on openstreetmap-ng codebase by about 1%.

14.6s → 14.4s (-1.1% ± 0.4%)

The generic getIndexContaining supports sparse arrays ((T | undefined)[]) and does extra defensive work. In our hot paths (token/line collections), the array is dense and ordered, so this version:

  • avoids undefined checks and extra branching
  • uses a tight binary search loop
  • adds a cheap “gap” early-out to avoid extra iterations when the position is clearly between adjacent ranges

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes TextRangeCollection.getItemContaining by replacing the generic sparse-array search with a specialized binary search tailored for dense, start-sorted TextRange arrays, aiming to reduce branching and improve hot-path performance.

Changes:

  • Switched getItemContaining to use a new dense-array search helper.
  • Added getIndexContainingDense with a tighter binary search loop and a “gap” early-out.

Comment on lines +104 to +106
// Fast path for dense, start-sorted ranges:
// binary-search for the containing item, with an early-out
// when the position falls between adjacent ranges.
Comment on lines +111 to +113
while (min <= max) {
const mid = min + ((max - min) >> 1);
const item = arr[mid];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants